<?php


namespace app\platform\controller;


use data\service\Article;
use data\service\EnterpriseRegistration;
use data\service\Family;

class FamilyCard extends BaseController
{
    /**
     * 页面list
     *
     * @return array|\think\response\View
     * @author dzl
     */
    public function index()
    {
        if (request()->isAjax()) {
            $article = new Family();

            $page_index = request()->post('page_index', 1);
            $page_size = request()->post('page_size', PAGESIZE);
            $str = request()->post('name', '');
            $status = request()->post('status', '');
            if (!empty($str)) {
                $condition['name'] = array(
                    "like",
                    "%" . $str . "%"
                );
            }

            if ($status != "") {
                $condition['is_status'] = $status;
            }
            $article_list = $article->getFamilyQuery($condition, $page_size = 0, $page_index = 0, $order = "");
            return $article_list;
        } else {
            return view($this->style . 'Family/list');
        }
    }

    /**
     * 添加亲友卡
     *
     * @return \multitype|\think\response\View
     * @author dzl
     */
    public function addFamilyCard()
    {
        if (request()->isAjax()) {
            $theme = request()->post('theme', '');
            $classic_flag = request()->post('classic_flag', '');
            $title = request()->post('title', '');
            $image = request()->post('image', '');
            $price = request()->post('price', '');
            $content = request()->post('content', '');
            $commend_flag = request()->post('commend_flag', '');
            $comment_count = request()->post('comment_count', '');

            $data = array(
                'theme' => $theme,
                'is_classic' => $classic_flag,
                'name' => $title,
                'background' => $image,
                'price'=>$price,
                'card_details' => $content,
                'is_status' => $commend_flag,
                'frequency' => $comment_count,
            );
            $service = new Family();
            $res = $service->addFamily($data);
            return AjaxReturn($res);
        } else {
            return view($this->style . 'Family/addFamilyCard');

        }

    }

    /**
     * 亲友卡详情
     *
     * @param $id
     * @return \think\response\View
     * @author dzl
     */
    public function view($id)
    {
        $article = new Family();
        $condition['id'] = $id;
        $article_list = $article->findFamily($condition);
        $image = $article_list['background'];
        $article_list['image'] = "<img src=" . $image . " style = \"width: 371px;height: 377px;\" class=\"lazy_load\" data-original=" . $image . " />";
        $article_list['created_at'] = date('Y-m-d H:i:s', $article_list['created_at']);
        $article_list['updated_at'] = date('Y-m-d H:i:s', $article_list['updated_at']);
        $status = $article_list['is_status'];
        if ($status == 1) {
            $article_list['str_status'] = "<b style=\"color: blue;\">开启</b>";

        } elseif ($status == 0) {
            $article_list['str_status'] = "<b style=\"color: grey;\">关闭</b>";

        }

        $this->assign('list', $article_list);
        return view($this->style . 'Family/view');
    }

    /**
     * 编辑亲友卡
     *
     * @param $id
     * @return \multitype|\think\response\View
     * @author dzl
     */
    public function editFamilyCard($id)
    {

        if (request()->isAjax()) {
            $theme = request()->post('theme', '');
            $classic_flag = request()->post('classic_flag', '');
            $title = request()->post('title', '');
            $image = request()->post('image', '');
            $price = request()->post('price', '');
            $content = request()->post('content', '');
            $commend_flag = request()->post('commend_flag', '');
            $comment_count = request()->post('comment_count', '');

            $data = array(
                'theme' => $theme,
                'is_classic' => $classic_flag,
                'name' => $title,
                'background' => $image,
                'price'=>$price,
                'card_details' => $content,
                'is_status' => $commend_flag,
                'frequency' => $comment_count,
            );
            $service = new Family();
            $res = $service->editFamily($data,$id);
            return AjaxReturn($res);
        }
        $article = new Family();
        $condition['id'] = $id;
        $article_list = $article->findFamily($condition);
        $image = $article_list['background'];
        $article_list['image'] = "<img src=" . $image . " style = \"width: 371px;height: 377px;\" class=\"lazy_load\" data-original=" . $image . " />";
        $article_list['created_at'] = date('Y-m-d H:i:s', $article_list['created_at']);
        $article_list['updated_at'] = date('Y-m-d H:i:s', $article_list['updated_at']);
        $status = $article_list['is_status'];
        if ($status == 1) {
            $article_list['str_status'] = "<b style=\"color: blue;\">开启</b>";

        } elseif ($status == 0) {
            $article_list['str_status'] = "<b style=\"color: grey;\">关闭</b>";

        }
        $this->assign('list', $article_list);
        return view($this->style . 'Family/editFamilyCard');

    }

}